<template>
  <div class="change-password">
    <el-form :model="passwordForm" ref="formRef" :rules="rules" label-width="100px">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input 
          v-model="passwordForm.oldPassword" 
          :type="passwordVisible.old ? 'text' : 'password'" 
          placeholder="请输入旧密码"
        >
          <template #suffix>
            <el-icon class="eye-icon" @click="passwordVisible.old = !passwordVisible.old">
              <View v-if="passwordVisible.old"/>
              <Hide v-else/>
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input 
          v-model="passwordForm.newPassword" 
          :type="passwordVisible.new ? 'text' : 'password'" 
          placeholder="请输入新密码"
        >
          <template #suffix>
            <el-icon class="eye-icon" @click="passwordVisible.new = !passwordVisible.new">
              <View v-if="passwordVisible.new"/>
              <Hide v-else/>
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmPassword">
        <el-input 
          v-model="passwordForm.confirmPassword" 
          :type="passwordVisible.confirm ? 'text' : 'password'" 
          placeholder="请确认新密码"
        >
          <template #suffix>
            <el-icon class="eye-icon" @click="passwordVisible.confirm = !passwordVisible.confirm">
              <View v-if="passwordVisible.confirm"/>
              <Hide v-else/>
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitPasswordChange">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { View, Hide } from '@element-plus/icons-vue'
import {updatePasswordAPI} from "@/apis/user";
import {useUserStore} from "@/stores/user";
import router from "@/router";

const formRef = ref();
const passwordForm = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
});

// 控制密码显示状态
const passwordVisible = ref({
  old: false,
  new: false,
  confirm: false
});

// 验证规则
const rules = {
  oldPassword: [
    { required: true, message: '请输入旧密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6-20个字符之间', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6-20个字符之间', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (value === passwordForm.value.oldPassword) {
          callback(new Error('新密码不能与旧密码相同'));
        } else {
          callback();
        }
      }, 
      trigger: 'blur'
    }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.value.newPassword) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ]
};

function submitPasswordChange() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      const newUserInfo=useUserStore().getUserInfo()
      newUserInfo.password=passwordForm.value.oldPassword
      newUserInfo.newPassword=passwordForm.value.newPassword
      await updatePasswordAPI(JSON.stringify(newUserInfo)).then(res=>{
        if(res.code==='200'){
          ElMessage.success('密码修改成功，请重新登录')
          useUserStore().clearUserInfo()
          router.push('/login')
        }else{
          ElMessage.error(res.msg)
        }
      })

    } else {
      //ElMessage.error('请正确填写表单信息');
      return false;
    }
  });
}
</script>

<style scoped>
.change-password {
  margin: 0 auto;
  padding: 20px;
}

.el-input {
  width: 300px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-button {
  margin-top: 10px;
}

.eye-icon {
  cursor: pointer;
}
</style> 